package servlet;

import java.awt.JobAttributes;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import com.mysql.jdbc.Util;
import com.wto.*;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.wto.CollegeBean;
import com.wto.IUtilSql;
import com.wto.ParseTeacherHtml;
import com.wto.Teacher;
import com.wto.TeacherBean;
import com.wto.UtilSqlFactory;
import com.wto.UtilTeacherSql;


@WebServlet("/TeacherServlet")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private CollegeBean collegeBean;
	private ParseTeacherHtml parseHtml;
	private List<TermBean> termList;
@Override
public void init() throws ServletException {
	System.out.println("init");
	collegeBean=new CollegeBean("0001","中原工学院","http://jw.zzti.edu.cn");
	parseHtml=new ParseTeacherHtml(collegeBean);
	termList=parseHtml.getIndex();
	
	System.out.println(UtilJson.convertToJsonArray(termList));
	super.init();
}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8");
		
		String type=request.getParameter("type");
		if(type.equals("getTermList"))
		{
			if(termList!=null)
			{
				PrintWriter out=response.getWriter();
				out.print(UtilJson.convertToJsonArray(termList));
				
			}
			return;
		}
		if(type.equals("getTeacherList"))
		{
			PrintWriter out=response.getWriter();
			String s_id=request.getParameter("s_id");
			List<TeacherBean> list=parseHtml.getTeachers(s_id);
			out.print(UtilJson.convertToJsonArray(list));
			return ;
		}
		if(type.equals("getCheckCode"))
		{
			response.setContentType("image/png");
		    byte[] bytes =parseHtml.getImageVer();
		    OutputStream stream=response.getOutputStream();
		    stream.write(bytes);
		    stream.flush();
		    stream.close();
		    return;
		}
		if(type.equals("getCourse"))
		{
			PrintWriter out=response.getWriter();
			String s_id=request.getParameter("s_id");
			String t_id=request.getParameter("t_id");
			String checkCode=request.getParameter("checkCode");
			TeacherBean bean=new TeacherBean();			
			bean.setCollege_id(collegeBean.getCollege_id());
			bean.setS_id(s_id);
			bean.setTeacherID(t_id);
			IUtilSql utilSql=new UtilSqlFactory().createUtilSql("teacher");
			Teacher teacher=(Teacher) utilSql.select(bean);
			if (teacher!=null) 
			{
				out.println(teacher.toJsonObject());
				System.out.println("数据库中有数据");
				return;
			}
			teacher=parseHtml.getTeacher(s_id, t_id, checkCode);
			//该教师有课程
			if(teacher!=null)
			{
				utilSql=new UtilSqlFactory().createUtilSql("teacher");
				utilSql.insert(teacher);
				out.print(teacher.toJsonObject());
				return;
			}
			out.print(new JSONObject());		
			
		}
//		String s_id;
//		String t_id;
//		String check_code;
//		
//		String College_id="0001";
//		String S_id=(String) request.getParameter("S_id");
//		String TeacherID=(String) request.getParameter("TeacherID");
//		String Check_Code=(String) request.getParameter("Check_Code");
//		System.out.println(S_id);
//		System.out.println(TeacherID);
//		System.out.println(Check_Code);
//		
//		TeacherBean bean=new TeacherBean();
//		
//		bean.setCollege_id(College_id);
//		bean.setS_id(S_id);
//		bean.setTeacherID(TeacherID);
////固定数据	
////		bean.setCollege_id("0001");
////		bean.setS_id("20161");
////		bean.setTeacherID("0000336");
//		IUtilSql utilSql=new UtilSqlFactory().createUtilSql("teacher");
//		Teacher teacher=(Teacher) utilSql.select(bean);
//		if (teacher!=null) {
//			out.println(teacher.toJsonObject());
//			System.out.println("数据库中有数据");
//		}else{
//			
//			
//			
//			parseHtml.getIndex();
//			parseHtml.getImage();
//			s_id=S_id;
//			parseHtml.getTeachers(s_id);
//			t_id=TeacherID;
//			
//			check_code=Check_Code;
//			Teacher teacheri =parseHtml.getTeacher(s_id, t_id, check_code);
//			IUtilSql utilSqli=new UtilTeacherSql();
//			utilSqli.insert(teacher);
//			out.println(teacheri.toJsonObject());
//			System.out.println("已下载完成");
//		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
